package com.jinrustar.op.core.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
//import org.jetbrains.annotations.NotNull;
//import org.jetbrains.annotations.Nullable;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public final class HttpUtil {
/*	private static final Logger logger = LogManager.getLogger();*/

    private HttpUtil() {
    }

    public static final String httpClientPost(String url) {
        String result = "";
        HttpClient client = new HttpClient();
        GetMethod getMethod = new GetMethod(url);
        try {
            client.executeMethod(getMethod);
            result = getMethod.getResponseBodyAsString();
        } catch (Exception e) {
            /*logger.error(e);*/
        } finally {
            getMethod.releaseConnection();
        }
        return result;
    }

    public static final String httpClientPost(String url, ArrayList<NameValuePair> list) {
        String result = "";
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        try {
            NameValuePair[] params = new NameValuePair[list.size()];
            for (int i = 0; i < list.size(); i++) {
                params[i] = list.get(i);
            }
            postMethod.addParameters(params);
            client.executeMethod(postMethod);
            result = postMethod.getResponseBodyAsString();
        } catch (Exception e) {
            /*logger.error(e);*/
        } finally {
            postMethod.releaseConnection();
        }
        return result;
    }


    public static final String httpClientGet(String url) {
        String result = "";
        HttpClient client = new HttpClient();
        GetMethod getMethod = new GetMethod(url);
        try {
            client.executeMethod(getMethod);
            result = getMethod.getResponseBodyAsString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            getMethod.releaseConnection();
        }
        return result;

    }


    static public ArrayList<NameValuePair> covertToNameValuePairsFromMap(Map<String, String> params) {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
        params.keySet().forEach(k -> {
            NameValuePair nameValuePair = new NameValuePair(k, params.get(k));
            nameValuePairs.add(nameValuePair);
        });
        return nameValuePairs;
    }


    /*static public String postByURL(String url, List<NameValuePair> data) {

    }*/

    public static String postByURL( String url, List<com.csource.common.NameValuePair> nameValuePairs) {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setDoOutput(true);
            OutputStream out = connection.getOutputStream();

            final StringBuilder dataStr = new StringBuilder();
            nameValuePairs.forEach(nameValuePair -> {
                dataStr.append(nameValuePair.getName() + "=" + nameValuePair.getValue() + "&");
            });

            System.out.println("####### " + dataStr);

            out.write(dataStr.toString().getBytes());
            out.flush();

            InputStream inputStream = connection.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while (-1 != (len = inputStream.read(buffer, 0, buffer.length))) {
                baos.write(buffer, 0, len);
            }
            return baos.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return "";
    }
}
